package com.fire.control.system.config.mqtt.subscribe.warning;


import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

import com.fire.control.system.statics.Type_Topic;


/**
* @ClassName: DataMqttClientFactory
* @Description: TODO(mqtt消息工厂- 报警)
* @author wanbo
* @date 2017年6月15日 下午3:10:24
*/
//@Configuration
//@EnableAutoConfiguration
public class MqttClientFactory_SubWarning {
	
	@Autowired private MqttMessageHandler_SubWarning mqttMessageHandler;
	
	@Value("${spring.mqtt.this.username}")
	private String username;
	@Value("${spring.mqtt.this.password}")
	private String password;
	@Value("${spring.mqtt.this.url}")
	private String url;
	private String client_id = UUID.randomUUID().toString().replace("-", "");
	private String topic = Type_Topic.sub_warning.getText();
	
	@ConfigurationProperties(prefix="spring.mqtt.this")  
	public DefaultMqttPahoClientFactory getDefaultMqttPahoClientFactory(){
		DefaultMqttPahoClientFactory defaultClientFactory = new DefaultMqttPahoClientFactory();
		defaultClientFactory.setUserName(username);
		defaultClientFactory.setPassword(password);
		defaultClientFactory.setServerURIs(url);
		return defaultClientFactory;
	}
	
	@Bean
	@ServiceActivator(inputChannel = "mqttInputChannel_subWarning")
	public MessageHandler handler_subWarning() {
		return mqttMessageHandler;
	}
	
	@Bean
    public MessageChannel mqttInputChannel_subWarning() {
        return new DirectChannel();
    }
	
	@Bean
    public MessageProducer inbound_subWarning() {
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(client_id,this.getDefaultMqttPahoClientFactory(), topic);
        adapter.setCompletionTimeout(5000);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        adapter.setOutputChannel(mqttInputChannel_subWarning());
        return adapter;
    }
}
